define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/house/houselayout/index' + location.search,
                    add_url: 'xiluhouse/house/houselayout/add',
                    edit_url: 'xiluhouse/house/houselayout/edit',
                    del_url: 'xiluhouse/house/houselayout/del',
                    multi_url: 'xiluhouse/house/houselayout/multi',
                    dragsort_url:'',
                    house_url:'xiluhouse/house/house/index',
                    housebuilding_url:'xiluhouse/house/housebuilding/index',
                    table: 'house_layout',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='house_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.house_url).data("primaryKey", "id").data("field", "name");
                $("input[name='house_building_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housebuilding_url).data("primaryKey", "id").data("field", "name");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'house_id', title: __('House_id'),formatter: function (value, row, index) {
                            if(row.house_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.house_url+ "?id=" + row.house_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.house_id +'】'+row.house.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        {field: 'house_building_id', title: __('House_building_id'),formatter: function (value, row, index) {
                            if(row.house_building_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.housebuilding_url+ "?id=" + row.house_building_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.house_building_id +'】'+row.housebuilding.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        
                        {field: 'name', title: __('基本信息'),operate:'LIKE',formatter: function (value, row, index) {
                            var html='';
                            html+='<div>'+ row.name+' '+ row.total_price+'万' +'</div>';
                            html+='<div>'+ row.price+'元/㎡  '+ row.space+'㎡ '+'</div>';
                            html+='<div>'+__('Face')+':'+ row.face +'</div>';
                            return html;
                        }},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images,operate:false},
                        {field: 'price', title: __('Price'), operate:'BETWEEN',visible:false},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN',visible:false},
                        {field: 'space', title: __('Space'), operate:'BETWEEN',visible:false},
                        {field: 'sale_state', title: __('Sale_state'), searchList: {"1":__('Sale_state 1'),"2":__('Sale_state 2'),"3":__('Sale_state 3')}, formatter: Table.api.formatter.normal},
                        {field: 'face', title: __('Face'),operate:'LIKE',visible:false},
                        {field: 'tags', title: __('标签'), formatter: Table.api.formatter.flag},
                        {field: 'room_num', title: __('Room_num'), operate:'BETWEEN',visible:false},
                        {field: 'ting_num', title: __('Ting_num'), operate:'BETWEEN',visible:false},
                        {field: 'wei_num', title: __('Wei_num'), operate:'BETWEEN',visible:false},
                        {field: 'floor_num', title: __('Floor_num'), operate:'BETWEEN',visible:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/house/houselayout/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/house/houselayout/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/house/houselayout/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});